import java.util.ArrayList;
import processing.core.PConstants;
import controlP5.ControlP5;

//View for displaying artists that the user has ignored.


public class IgnoreView extends View {
	ControlP5 ControlP5;
	ArrayList<controlP5.Button> Buttons;
	
	private int xOffset = 25;
	private int yOffset = 35;
	private int cOffset = 190;
	
	public IgnoreView(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		setup();
	}
	
	private void setup() {
		if(GLOBAL.IGNORE.isEmpty())
			return;
		
		ControlP5 = new ControlP5(GLOBAL.processing);
			ControlP5.setAutoDraw(false);
		Buttons = new ArrayList<controlP5.Button>();
		
		int x_ = x1+xOffset;
		int y_ = y1+yOffset;
		
		for(int x=0; x<GLOBAL.IGNORE.size(); ++x) {
			controlP5.Button b = ControlP5.addButton("vw_remove:" + GLOBAL.IGNORE.get(x).getName(), 0, x_, y_, 16, 16);
				b.setImages(GLOBAL.processing.loadImage("ex1.gif"), GLOBAL.processing.loadImage("ex2.gif"), GLOBAL.processing.loadImage("ex3.gif"));
			Buttons.add(b);
				
			y_ += yOffset;
			
			if(y_ >= y2-10) {
				y_ = y1+yOffset;
				x_ += cOffset;
			}
		}
		
	}
	
	public void draw() {
		GLOBAL.processing.fill(0);
		GLOBAL.processing.textAlign(PConstants.CENTER);
		
		if(GLOBAL.IGNORE.isEmpty()) {
			GLOBAL.processing.text("You Have Not Ignored Any Artists", ((x1+x2)/2), y1+50);
		}
		else {
			ControlP5.draw();
			GLOBAL.processing.text("Artists You Are Ignoring", ((x1+x2)/2), y1+25);
			
			int x_ = x1+xOffset;
			int y_ = y1+yOffset;
			
			for(int x=0; x<GLOBAL.IGNORE.size(); ++x) {
				GLOBAL.processing.textAlign(PConstants.LEFT);
				GLOBAL.processing.text(GLOBAL.IGNORE.get(x).getName(), x_+22, y_+12);
				Buttons.get(x).setPosition(x_, y_);
				
				y_ += yOffset;
				
				if(y_ >= y2-10) {
					y_ = y1+yOffset;
					x_ += cOffset;
				}
			}
		}
	}
	
	public void handleEvent(String event) {
		//User wishes to remove artist from IGNORE list
		//	Name of artist is stored in the buttton name after the first index of ':'
		String name = event.substring(event.indexOf(":")+1);	//Get Artist Name
		GLOBAL.removeByName(name);	//Remove Artist from IGNORE list
		ControlP5.remove(event);	//Remove Button from controller
		Buttons.remove(getButtonIndex(event));	//Remove Button from ArrayList
		
	}

	private int getButtonIndex(String name) {
		for(int x=0; x<Buttons.size(); ++x) {
			if(Buttons.get(x).name().equals(name)) {
				return x;
			}
		}
		
		return -1;
	}
	
}
